<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
       
        <BR>
        <?php
        include "ModelClass.php";



         class simple_profiler {

                private $start_time;

                private function get_time() {
                    list($usec, $seconds) = explode(" ", microtime());
                    return ((float)$usec + (float)$seconds);
                }
                function start_timer() {
                    $this->start_time = $this->get_time();
                }
                function end_timer() {
                    return ($this->get_time() - $this->start_time);
                }
            }

        //----------------------------------------------------------------------

        function conecta($dbhost,$dbusername,$dbpass,$database){
            $link=DB::connect($dbhost,$dbusername,$dbpass);
            if (!$link) {
                die('Could not connect: ' . mysql_error());
                return FALSE;
            }
            if(!DB::select_db($database,$link)){
                    DB::close($link);
                    die('Could not connect: ' . mysql_error());
                    return FALSE;
            }
            return $link;
        }

        //----------------------------------------------------------------------

        function reinicia(){
            $query= array(  'DROP TABLE data;',
                            'DROP TABLE structure;',
                            'DROP TABLE elements',
                            //Tabla estructura
                            "CREATE TABLE structure (
                                id INT(11) AUTO_INCREMENT PRIMARY KEY,
                                value VARCHAR(100) NOT NULL,
                                type VARCHAR(20) NOT NULL,
                                lft INT(6) NOT NULL,
                                rgt INT(6) NOT NULL
                            );",
                            //Indice sobre value
                            "CREATE UNIQUE INDEX IDX_STRUCT ON structure (value);",
                            //Tabla datos
                            "CREATE TABLE data(
                                id INT(11)NOT NULL,
                                id_structure INT(11)NOT NULL,
                                value VARCHAR(100)NOT NULL,
                                PRIMARY KEY(id,id_structure),
                                FOREIGN KEY (id_structure)
                                REFERENCES structure(id),
                                FOREIGN KEY (id)
                                REFERENCES elements(id)
                            );",
/*
                            "ALTER TABLE data ADD CONSTRAINT ref1
                            FOREIGN KEY (id) REFERENCES element(id);",

                            "ALTER TABLE data ADD CONSTRAINT ref2
                            FOREIGN KEY (id_structure) REFERENCES structure(id);",
*/
                            //sobra este indice (primary key index)
                            //"CREATE UNIQUE INDEX IDX_DATA ON data (id,id_structure);",
                            //Tabla elementos
                            "CREATE TABLE elements(
                                type CHAR(20),
                                id INT (11) AUTO_INCREMENT PRIMARY KEY ,
                                name VARCHAR(50)
                            );",
                            "CREATE INDEX IDX_ELEMENTS ON elements(name) "
                            );

            foreach ($query as $q) DB::query($q);

        }
        //----------------------------------------------------------------------

        function creaEstructura($model,$debug){
            //RAIZ
           // StructHandler::addChild('SISTEMA',"node","","");

            //PRIMER NIVEL
            $model->addChild('USR',"element","","",$debug);
            $model->addChild('RSC',"element","","",$debug);
            $model->addChild('USR-RSC',"element#2","","",$debug);
            $model->addChild('GLO',"element","","",$debug);
            $model->addChild('SAL',"element","","",$debug);
            $model->addChild('SER',"element","","",$debug);
            $model->addChild('USR-GLO',"element#2","","",$debug);
            $model->addChild('USR-SAL',"element#2","","",$debug);
            $model->addChild('USR-SER',"element#2","","",$debug);

            $model->addChild('USR-RSC-GLO',"element#3","","",$debug);
            $model->addChild('USR-RSC-SAL',"element#3","","",$debug);
            $model->addChild('USR-RSC-SER',"element#3","","",$debug);
            $model->addChild('SAL-RSC-USR',"element#3","","",$debug);

            $model->addChild('USR-RSC-GLO-SAL',"element#4","","",$debug);
            $model->addChild('USR-RSC-GLO-SER',"element#4","","",$debug);
            $model->addChild('USR-RSC-SAL-SER',"element#4","","",$debug);
            $model->addChild('GLO-SAL-SER-USR',"element#4","","",$debug);

            $model->addChild('GLO-SAL-SER-USR-RSC',"element#5","","",$debug);

            $model->addChild('ATRIBUTO',"integer","/USR-RSC","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-SAL","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-SER","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-GLO","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-GLO","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-SAL","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-SER","",$debug);
            $model->addChild('ATRIBUTO',"integer","/SAL-RSC-USR","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-GLO-SAL","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-GLO-SER","",$debug);
            $model->addChild('ATRIBUTO',"integer","/USR-RSC-SAL-SER","",$debug);
            $model->addChild('ATRIBUTO',"integer","/GLO-SAL-SER-USR","",$debug);
            $model->addChild('ATRIBUTO',"integer","/GLO-SAL-SER-USR-RSC","",$debug);



            //SEGUNDO NIVEL
            $model->addChild('LOGIN',"node","/USR","",$debug);
            $model->addChild('GESTION',"node","","/USR/LOGIN",$debug);
            $model->addChild('ACTIVIDAD',"node","","/USR/GESTION",$debug);


            $model->addChild('LOCALIZACION',"node","/RSC","",$debug);
            $model->addChild('CATEGORIZACION',"node","/RSC","",$debug);

            $model->addChild('NOTA',"integer","/USR-RSC","",$debug);
            $model->addChild('COMENTARIOS',"string","/USR-RSC","",$debug);

            //TERCER NIVEL
            $model->addChild('USERNAME',"string","/USR/LOGIN","",$debug);
            $model->addChild('PASSWORD',"string","/USR/LOGIN","",$debug);

            $model->addChild('E_MAIL',"string","/USR/GESTION","",$debug);
            $model->addChild('NOMBRE_REAL',"string","/USR/GESTION","",$debug);

            $model->addChild('FECHA_REGISTRO',"date","/USR/ACTIVIDAD","",$debug);
            $model->addChild('ULTIMO_ACCESO',"date","/USR/ACTIVIDAD","",$debug);



            $model->addChild('PLATAFORMA',"string","/RSC/LOCALIZACION","",$debug);
            $model->addChild('ID_EXTERNO',"integer","/RSC/LOCALIZACION","",$debug);
            $model->addChild('ENTORNO_PROFESIONAL',"node","/RSC/CATEGORIZACION","",$debug);
            $model->addChild('ENTORNO_ACADEMICO',"node","/RSC/CATEGORIZACION","",$debug);

            //CUARTO NIVEL


            $model->addChild('AMBITO_LABORAL',"string","/RSC/CATEGORIZACION/ENTORNO_PROFESIONAL","",$debug);
            $model->addChild('SECTOR',"string","/RSC/CATEGORIZACION/ENTORNO_PROFESIONAL","",$debug);
            $model->addChild('PUESTOS_DE_TRABAJO',"string","/RSC/CATEGORIZACION/ENTORNO_PROFESIONAL","",$debug);
            $model->addChild('NIVEL',"integer","/RSC/CATEGORIZACION/ENTORNO_ACADEMICO","",$debug);
            $model->addChild('AREA_DE_CONOCIMIENTO',"string","/RSC/CATEGORIZACION/ENTORNO_ACADEMICO","",$debug);






            return true;
        }

        //----------------------------------------------------------------------

        function muestraArbol(){
            $result=DB::query("SELECT node.lft, node.rgt, node.type,CONCAT( REPEAT( '__', (COUNT(parent.value) - 1) ), node.value) AS value
                                FROM structure AS node,
                                structure AS parent
                                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                                GROUP BY node.value
                                ORDER BY node.lft;");

            while ($r1=DB::fetch_assoc($result)){
                $fields= explode ('/',$r1['value']);
                $nc=count($fields);
                $n=array();
                for($i=0;$i<$nc-1;$i++){
                    $n[$i]=$fields[$i];
                }
                $n1=implode('/',$n);
                echo "<font color='#FF0000'>$n1</font>";
                echo "<font color='#0000CC'>/". $fields[$nc-1] ."</font>";
                echo "<font color='#339966'> ". $r1['type']."</font>";
                echo "<font color='#FF0000'> (".$r1['lft']."|".$r1['rgt'].") </font><BR>";
            }

        }

        // put your code here

        //CONEXION A LA BASE DE DATOS
$dbhost="localhost";
$dbusername="root";
$dbpass="";
$database="pruebas";

$debug=true;
//OPCIONES:
$reiniciar=false; //Borrar y crear tablas
$reiniciarEstructura=false;
$probar=true; //a falso no se hace la parte del codigo a probar




//CONEXION A BASE DE DATOS
$timer=new simple_profiler();
echo "1.-CONECTANDO A BASE DE DATOS ";
$timer->start_timer();
    $link=conecta($dbhost,$dbusername,$dbpass,$database);
$tiempo=$timer->end_timer();
echo " | CONECTADO: tiempo: $tiempo <BR>";

//CREAR OBJETO MODEL(despues de conectar, antes de modificar)
echo "2.-CREANDO OBJETO MODEL ";
$model=new Model($debug);
echo " | CREADO.<BR>";

  muestraArbol();
if($probar){

    reinicia();

    $model->initializeStruct();

    creaEstructura($model,false);

    muestraArbol();


    $h=$model->insertElement('USR', "PEDRITO",false); if($h<1) echo "UUUUUUUY1";

    $h=$model->insertElement('USR', "JOSITO",false); if($h<1) echo "UUUUUUUY2";
    $h=$model->insertElement('USR', "MANOLITO",false); if($h<1) echo "UUUUUUUY3";


    $h=$model->insertElement('RSC', "CURSILLO1",false); if($h<1) echo "UUUUUUUY4";
    $h=$model->insertElement('RSC', "CURSILLO2",false); if($h<1) echo "UUUUUUUY5";
    $h=$model->insertElement('RSC', "CURSILLO3",false); if($h<1) echo "UUUUUUUY6";

    $id1=$model->insertElement('SAL',"SAL1");
    $id2=$model->insertElement('GLO',"GLO1");
    $id3=$model->insertElement('SER',"SER1");

    $id4=$model->insertElement('SAL',"SAL2");
    $id5=$model->insertElement('GLO',"GLO2");
    $id6=$model->insertElement('SER',"SER2");

    $id7=$model->insertElement('SAL',"SAL3");
    $id8=$model->insertElement('GLO',"GLO3");
    $id9=$model->insertElement('SER',"SER3");

    $id10=$model->insertElement('SAL',"SAL4");
    $id11=$model->insertElement('GLO',"GLO4");
    $id12=$model->insertElement('SER',"SER4");


$model->createRelation('USR-1/RSC-4',$debug);
    $model->createRelation('USR-1/SAL-'.$id1,$debug);
    $model->createRelation('USR-2/GLO-'.$id2,$debug);
    $model->createRelation('USR-3/RSC-4/GLO-'.$id2,$debug);
    $model->createRelation('USR-3/RSC-4/SAL-'.$id1,$debug);
    $model->createRelation('USR-3/RSC-4/SER-'.$id3,$debug);
    $model->createRelation('SAL-'.$id1.'/RSC-4/USR-1',$debug);
    $model->createRelation('USR-1/RSC-4/GLO-'.$id2.'/SAL-'.$id1,$debug);
    $model->createRelation('USR-1/RSC-4/GLO-'.$id2.'/SER-'.$id3,$debug);
    $model->createRelation('USR-1/RSC-4/SAL-'.$id1.'/SER-'.$id3,$debug);
    $model->createRelation('GLO-'.$id2.'/SAL-'.$id1.'/SER-'.$id3.'/USR-1',$debug);
    $model->createRelation('GLO-'.$id2.'/SAL-'.$id1.'/SER-'.$id3.'/USR-1/RSC-4',$debug);


    
    $h=$model->createRelation("USR-1/RSC-4",true); if($h<1) echo "UUUUUUUY7";
    $h=$model->createRelation("USR-1/RSC-5",true); if($h<1) echo "UUUUUUUY8";
    $h=$model->createRelation("USR-1/RSC-5",true); if($h<1) echo "UUUUUUUY9";
    $h=$model->createRelation("USR-2/RSC-6",true); if($h<1) echo "UUUUUUUY10";
    $h=$model->createRelation("USR-3/RSC-6",true); if($h<1) echo "UUUUUUUY11";
    $h=$model->createRelation("USR-4/RSC-5",true); if($h<1) echo "UUUUUUUY12";
    $h=$model->createRelation("USR-1324/RSC-3244",true); if($h<1) echo "UUUUUUUY13";

    $h=$model->setValue("/RSC-4/CATEGORIZACION/ENTORNO_PROFESIONAL/SECTOR","valor1",false); if($h<1) echo "UUUUUUUY14";
    $h=$model->setValue("/RSC-5/CATEGORIZACION/ENTORNO_PROFESIONAL/SECTOR","valor2",false); if($h<1) echo "UUUUUUUY15";
    $h=$model->setValue("/RSC-6/CATEGORIZACION/ENTORNO_PROFESIONAL/SECTOR","valor3",false); if($h<1) echo "UUUUUUUY16";
    $h=$model->setValue("/RSC-1/CATEGORIZACION/ENTORNO_PROFESIONAL/SECTOR","valor3",false); if($h<1) echo "UUUUUUUY17";
    $h=$model->setValue("/RSC-125/CATEGORIZACION/ENTORNO_PROFESIONAL/SECTOR","valor3",false); if($h<1) echo "UUUUUUUY18";



    $h=$model->setValue("/USR-1/LOGIN/PASSWORD","valor1",false); if($h<1) echo "UUUUUUUY19";
    $h=$model->setValue("/USR-2/LOGIN/PASSWORD","valor1",false); if($h<1) echo "UUUUUUUY20";
    $h=$model->setValue("/USR-3/LOGIN/PASSWORD","valor1",false); if($h<1) echo "UUUUUUUY21";
    $h=$model->setValue("/USR-4/LOGIN/PASSWORD","valor1",false); if($h<1) echo "UUUUUUUY22";
    $h=$model->setValue("/USR-185/LOGIN/PASSWORD","valor1",false); if($h<1) echo "UUUUUUUY23";


    $h=$model->setValue("/USR-1/RSC-5/NOTA","valor1",false); if($h<1) echo "UUUUUUUY24";
    $h=$model->setValue("/USR-3/RSC-6/NOTA","valor1",false); if($h<1) echo "UUUUUUUY25";
    $h=$model->setValue("/USR-4/RSC-5/NOTA","valor1",false); if($h<1) echo "UUUUUUUY26";






    $buscar="USR-?/RSC-?";
    $r=$model->getRelation($buscar);
    echo "<BR>Buscando $buscar <BR>";
    foreach ($r as $r1){
        echo "$r1 <BR>";
    }
    $buscar="USR-1/RSC-?";
    $r=$model->getRelation($buscar);
    echo "Buscando $buscar <BR>";
    foreach ($r as $r1){
        echo "$r1 <BR>";
    }
    $buscar="GLO-?/SAL-?/SER-?";
    $r=$model->getRelation($buscar);
    echo "Buscando $buscar <BR>";
    foreach ($r as $r1){
        echo "$r1 <BR>";
    }
    $buscar="GLO-8/SAL-?/SER-9/USR-?/RSC-4";
    $r=$model->getRelation($buscar);
    echo "Buscando $buscar <BR>";
    foreach ($r as $r1){
        echo "$r1 <BR>";
    }





}

dB::close($link);

?>
    </body>
</html>
